package com.alit.face.koala.controller;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import com.alibaba.fastjson.JSONObject;
import com.alit.face.koala.entity.KoalaHost;
import com.alit.face.koala.repository.KoalaHostRepository;

/**
 * @author litong
 * @date 2019年5月10日_下午6:23:15
 * @version 1.0
 */
@RestController
@RequestMapping("koala_host")
public class KoalaHostController {

  @Autowired
  private KoalaHostRepository koalaHostRepository;

  @RequestMapping("list")
  public String list() {
    List<KoalaHost> all = koalaHostRepository.findAll();
    JSONObject jsonObject = new JSONObject();
    jsonObject.put("total", all.size());
    jsonObject.put("rows", all);
    return jsonObject.toJSONString();
  }

  @RequestMapping({ "save", "update" })
  public String save(KoalaHost koalaHost) {
    try {
      koalaHostRepository.save(koalaHost);
      return "{'success':true}";
    } catch (Exception e) {
      return "{'msg'," + e.getMessage() + "}";
    }
  }

  @RequestMapping("destroy")
  public String destroy(Integer id) {
    try {
      koalaHostRepository.deleteById(id);
      return "{'success':true}";
    } catch (Exception e) {
      return "{'msg'," + e.getMessage() + "}";
    }
  }
}
